package org.mnswf.net {
	
	import com.adobe.serialization.json.JSON;
	import flash.net.getClassByAlias;
	import flash.utils.getQualifiedClassName;
	import mx.utils.ObjectUtil;
	
	/**
	 * @author Danny Patterson
	 */
	public class UDPPacket {
		
		public var message:IUDPMessage;
		
		/**
		 * @param message (String) [optional]
		 */
		public function UDPPacket(packet:String = null) {
			if(packet) {
				var decodedPacket:Object = JSON.decode(packet);
				var MessageType:Class = getClassByAlias(decodedPacket.type);
				delete decodedPacket.type;
				message = IUDPMessage(new MessageType());
				for(var property:String in decodedPacket) {
					message[property] = decodedPacket[property];
				}
			}
		}
		
		/**
		 * @return (String)
		 */
		public function toString():String {
			var packet:Object = message.toObject();
			packet.type = getQualifiedClassName(message);
			return JSON.encode(packet).toString();
		}
		
	}
	
}